package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.LeagueClassDto;
import com.woniu.model.dto.LeagueWorkforceDto;
import com.woniu.model.form.AddLeagueClassForceForm;
import com.woniu.model.form.LoadLeagueClassForceForm;
import com.woniu.model.model.YLeagueClass;
import com.woniu.model.model.YLeagueWorkforce;
import com.woniu.model.param.AddLeagueClassForceParam;
import com.woniu.model.param.LoadLeagueClassForceParam;
import com.woniu.service.YLeagueWorkforceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yLeagueWorkforce")
@Api(tags = "团课排班")
public class YLeagueWorkforceController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private YLeagueWorkforceService leagueWorkforceService;

    /**
     * 场馆查询自己已经排班的团课
     * @param token
     * @param form
     * @return
     */
    @PostMapping("/loadListLeagueClass")
    @ApiOperation("根据场馆id查询已排班的团课")
    public Result<List<YLeagueWorkforce>> loadLeagueClassForceByVenueId(@RequestHeader String token,@RequestBody LoadLeagueClassForceForm form) {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        LoadLeagueClassForceParam param = BeanUtil.toBean(form, LoadLeagueClassForceParam.class);
        param.setVenueId(venueId);

        List<YLeagueWorkforce> list = leagueWorkforceService.loadLeagueClassForceByVenueId(param);
        return Result.newListInstance(YLeagueWorkforce.class).setCode(200).setMsg("查询成功").setData(list);
    }

    /**
     * 团课排班新增
     * @param token
     * @param form
     * @return
     */
    @PostMapping("/addLeagueClassForce")
    @ApiOperation("排班团课")
    public Result<?> addLeagueClassForce(@RequestHeader String token,@RequestBody AddLeagueClassForceForm form) throws Exception {

        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        AddLeagueClassForceParam param = BeanUtil.toBean(form, AddLeagueClassForceParam.class);
        param.setVenueId(venueId);
//        System.out.println(param);
        leagueWorkforceService.addLeagueClassForce(param);
//        if(!i.equals(1)){
//            return Result.getResult().setCode(500).setMsg("新增失败");
//        }

        return Result.getResult().setMsg("团课排班新增成功");
    }

    /**
     * 查询该场馆此时段可以排班的课程
     * @param token
     * @param form
     * @return
     */
    @PostMapping("/loadLeagueClassForceWhichAllow")
    @ApiOperation("查询该场馆此时段可以排班的课程")
    public Result<List<LeagueClassDto>> loadLeagueClassForceWhichAllow(@RequestHeader String token,@RequestBody LoadLeagueClassForceForm form) {
//        System.out.println(form.getStartTime());
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        LoadLeagueClassForceParam param = new LoadLeagueClassForceParam();
        param.setVenueId(venueId);
//        param.setVenueId(1);
        param.setStartTime(form.getStartTime());
        param.setEndTime(form.getEndTime());

        List<LeagueClassDto> list = leagueWorkforceService.loadLeagueClassForceWhichAllow(param);
        return Result.newListInstance(LeagueClassDto.class).setCode(200).setMsg("查询成功").setData(list);
    }

    /**
     * 教练确认完成团课
     * @param token
     * @param id 团课排班的id
     * @return
     */
    @PostMapping("/finishLeagueClassForce")
    @ApiOperation("教练确认完成团课")
    public Result<?> finishLeagueClassForce(@RequestHeader String token,@RequestBody Integer id) {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());


        Integer i = leagueWorkforceService.finishLeagueClassForce(coachId,id);
        if(!i.equals(1)){
            return Result.getResult().setMsg("团课完成失败").setCode(500);
        }
        return Result.getResult().setMsg("团课完成成功");
    }

    /**
     * 教练取消团课排班
     * @param token
     * @param id 团课排班的id
     * @return
     */
    @GetMapping("/cancelLeagueClassForce")
    @ApiOperation("教练取消团课排班")
    public Result<?> cancelLeagueClassForce(@RequestHeader String token, Integer id) {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());


        Integer i = leagueWorkforceService.cancelLeagueClassForce(coachId,id);
        if(!i.equals(1)){
            return Result.getResult().setMsg("教练取消团课排班失败").setCode(500);
        }
        return Result.getResult().setMsg("教练取消团课排班成功");
    }

    /**
     * 根据课程id查询排班信息
     * @param courseId
     * @return
     */
    @GetMapping("/loadLeagueWorkforceByClassId")
    @ApiOperation("根据课程id查询排班信息")
    Result<List<YLeagueWorkforce>> loadLeagueWorkforceByClassId(Integer courseId){
        List<YLeagueWorkforce> list = leagueWorkforceService.loadLeagueWorkforceByClassId(courseId);
        return Result.newListInstance(YLeagueWorkforce.class).setData(list).setMsg("查询排班信息成功");
    }

    /**
     * 根据课程id查询排班信息
     * @param token
     * @return
     */
    @GetMapping("/loadLeagueWorkforceByCoach")
    @ApiOperation("根据教练id查询未完成的团课")
    Result<List<LeagueWorkforceDto>> loadLeagueWorkforceByCoach(String className,@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        List<LeagueWorkforceDto> list = leagueWorkforceService.loadLeagueWorkforceByCoach(coachId,className);
        return Result.newListInstance(LeagueWorkforceDto.class).setData(list).setMsg("根据教练id查询未完成的团课成功");
    }
}

